//
//  HeroHelper.m
//  MGLOL盒子
//
//  Created by lanou3g on 15/12/3.
//  Copyright © 2015年 张明. All rights reserved.
//
#import "HeroModel.h"
#import "HeroHelper.h"
#import "AFHTTPSessionManager.h"
static HeroHelper *heroHelp = nil;
@implementation HeroHelper
- (void)getHeroDataWithUrl:(NSString *)url Key:(NSString *)key successData:(void (^)(NSMutableArray *array))success fail:(void (^)(NSError *err))fail {
    //判断URL是否为空
    if (!url) {
        NSError *err = [NSError errorWithDomain:@"Url为空" code:1 userInfo:nil];
        fail(err);
        return;
    }
    //遍历数组，封装数据
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
//                   NSLog(@"%@",responseObject);
        NSDictionary *dic = [responseObject objectForKey:key];
        _allDataMutableArr = [[NSMutableArray alloc]init];
        for (NSDictionary* d in dic) {
            HeroModel *model = [[HeroModel alloc]init];
            [model setValuesForKeysWithDictionary:d];
            [_allDataMutableArr addObject:model];
//           NSLog(@"-------%@",model.cnName);
        }
        success(_allDataMutableArr);
//        NSLog(@"!!!!!!!!%@",_allDataMutableArr);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)getHeroDataWithUrl2:(NSString *)url Key:(NSString *)key successData:(void (^)(id))success fail:(void (^)(NSError *err))fail {
    //判断URL是否为空
    if (!url) {
        NSError *err = [NSError errorWithDomain:@"Url为空" code:1 userInfo:nil];
        fail(err);
        return;
    }
    //遍历数组，封装数据
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"application/x-javascript", nil];
    [manager GET:url parameters:key progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
//              NSLog(@"success --responseObject- %@", responseObject);
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

+(instancetype)shareHeroWithHelpData {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        heroHelp = [[HeroHelper alloc]init];
    });
    
    return heroHelp;
}
@end
